<template>
	<div v-loading="fullscreenLoading">
		<!--收货地址-->
		<div class="item-block" v-if="orderPaymentData.is_virtual == 0">
			<div class="block-text">收货地址</div>

			<div clsas="address-list">
				<div class="address-item" @click="addAddressShow">
					<div class="add-address">
						<i class="el-icon-circle-plus-outline"></i>
						添加收货地址
					</div>
				</div>

				<div
					class="address-item"
					v-for="(item, key) in memberAddress"
					:key="item.id"
					:class="addressId == item.id ? 'active' : ''"
					v-if="key < 3 || (addressShow && key >= 3)"
				>
					<div class="address-info">
						<div class="options">
							<div @click="editAddress(item.id)">编辑</div>
							<template v-if="item.is_default == 0">
								<el-popconfirm title="确定要删除该地址吗？" @onConfirm="deleteMemberAddress(item.id)"><div slot="reference">删除</div></el-popconfirm>
							</template>
						</div>
						<div class="address-name">{{ item.name }}</div>
						<div class="address-mobile" @click="setMemberAddress(item.id)">{{ item.mobile }}</div>
						<div class="address-desc" @click="setMemberAddress(item.id)">{{ item.full_address }} {{ item.address }}</div>
					</div>
				</div>

				<div v-if="memberAddress.length > 3 && !addressShow" @click="addressShow = true" class="el-button--text address-open">
					<i class="el-icon-arrow-down"></i>
					更多收货地址
				</div>

				<div class="clear"></div>
			</div>
		</div>

		<!--购买虚拟类商品需填写您的手机号-->
		<div class="item-block" v-if="orderPaymentData.is_virtual == 1">
			<div class="block-text">购买虚拟类商品需填写您的手机号，以方便商家与您联系</div>

			<el-form ref="form" size="mini" class="mobile-wrap" label-width="80px">
				<el-form-item label="手机号码"><el-input placeholder="请输入您的手机号码" maxlength="11" v-model="orderCreateData.member_address.mobile"></el-input></el-form-item>
			</el-form>
		</div>

		<!--收货地址添加-->
		<el-dialog :title="addressForm.id == 0 ? '添加收货地址' : '编辑收货地址'" :visible.sync="dialogVisible" width="32%">
			<el-form ref="form" :rules="addressRules" :model="addressForm" label-width="80px">
				<el-form-item label="姓名" prop="name"><el-input v-model="addressForm.name" placeholder="收货人姓名"></el-input></el-form-item>

				<el-form-item label="手机" prop="mobile"><el-input v-model="addressForm.mobile" maxlength="11" placeholder="收货人手机号"></el-input></el-form-item>

				<el-form-item label="电话"><el-input v-model="addressForm.telephone" placeholder="收货人固定电话（选填）"></el-input></el-form-item>

				<el-form-item class="area" label="地区" prop="area">
					<el-row :gutter="10">
						<el-col :span="7">
							<el-select prop="province" ref="province" v-model="addressForm.province_id" @change="getAddress(1)" placeholder="请选择省">
								<el-option label="请选择省" value="0"></el-option>
								<el-option v-for="item in pickerValueArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-col>
						<el-col :span="7">
							<el-select ref="city" prop="city" v-model="addressForm.city_id" @change="getAddress(2)" placeholder="请选择市">
								<el-option label="请选择市" value="0"></el-option>
								<el-option v-for="item in cityArr" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-col>
						<el-col :span="7">
							<el-select ref="district" prop="district" v-model="addressForm.district_id" placeholder="请选择区/县">
								<el-option label="请选择区/县" value="0"></el-option>
								<el-option v-for="item in districtArr" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-col>
					</el-row>
				</el-form-item>

				<el-form-item label="详细地址" prop="address"><el-input v-model="addressForm.address" placeholder="定位小区、街道、写字楼"></el-input></el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="addmemberAddress('form')">确 定</el-button>
			</span>
		</el-dialog>

		<!--使用余额-->
		<div class="item-block" v-if="orderPaymentData.member_account.balance_total > 0">
			<div class="block-text">是否使用余额</div>

			<div class="pay-type-list">
				<div class="pay-type-item" :class="orderCreateData.is_balance ? '' : 'active'" @click="useBalance(0)">不使用余额</div>
				<div class="pay-type-item" :class="orderCreateData.is_balance ? 'active' : ''" @click="useBalance(1)">使用余额</div>
				<div class="clear"></div>
			</div>
		</div>

		<!--商品信息-->
		<div class="item-block">
			<div class="goods-list">
				<table>
					<tr>
						<td width="50%">商品</td>
						<td width="12.5%">价格</td>
						<td width="12.5%">数量</td>
						<td width="12.5%">小计</td>
					</tr>
				</table>
			</div>
		</div>

		<div v-for="(siteItem, siteIndex) in orderPaymentData.shop_goods_list" :key="siteIndex">
			<div class="item-block">
				<div class="goods-list">
					<table>
						<tr>
							<td colspan="5">
								<i class="store-icon iconfont iconmendian"></i>
								<router-link :to="'/shop-' + siteItem.site_id" target="_blank">{{ siteItem.site_name }}</router-link>
							</td>
						</tr>
					</table>
				</div>
			</div>
			<div class="item-block">
				<div class="goods-list">
					<table>
						<!-- v-if="goodsItem.promotion_type != 'nthmfolddiscount'" -->
						<tr v-for="(goodsItem, goodsIndex) in siteItem.goods_list" :key="goodsIndex" v-if="goodsItem.promotion_type != 'nthmfolddiscount'" >
							<td width="50%">
								<div class="goods-info">
									<div class="goods-info-left">
										<router-link :to="{ path: '/sku-' + goodsItem.sku_id }" target="_blank">
											<img class="goods-img" :src="$img(goodsItem.sku_image, { size: $goodsImgType('mid',goodsItem.source) })" @error="imageError(goodsIndex)" />
										</router-link>
									</div>
									<div class="goods-info-right">
										<router-link :to="{ path: '/sku-' + goodsItem.sku_id }" target="_blank">
											<div class="goods-name">{{ goodsItem.sku_name }}</div>
										</router-link>
									</div>
								</div>
							</td>
							<td width="12.5%" class="goods-price">￥{{ goodsItem.discount_price }}</td>
							<td width="12.5%" class="goods-num">{{ goodsItem.num }}</td>
							<td width="12.5%" v-if="goodsIndex == 0" :rowspan="siteItem.goods_list.length" class="goods-money">￥{{ siteItem.pay_money | moneyFormat }}</td>
						</tr>
						<tr v-for="(goodsItem, goods_Index) in siteItem.goods_list" :key="goods_Index" v-if="goodsItem.promotion_type == 'nthmfolddiscount'">
							<span v-for="(item, index) in goodsItem.discount_price_data" :key="index" class="goods-heads">
							<td width="50%">
								<div class="goods-info">
									<div class="goods-info-left">
										<router-link :to="{ path: '/sku-' + goodsItem.sku_id }" target="_blank">
											<img class="goods-img" :src="$img(goodsItem.sku_image, { size: $goodsImgType('mid',goodsItem.source) })" @error="imageError(goods_Index)" />
										</router-link>
									</div>
									<div class="goods-info-right">
										<router-link :to="{ path: '/sku-' + goodsItem.sku_id }" target="_blank">
											<div class="goods-name">{{ goodsItem.sku_name }}</div>
										</router-link>
									</div>
								</div>
							</td>
							<td width="12.5%" class="goods-price">￥{{ item.discount_price }}</td>
							<td width="12.5%" class="goods-num">{{ item.num }}</td>
							<!-- <td width="12.5%" v-if="goodsIndex == 0" :rowspan="siteItem.goods_list.length" class="goods-money">￥{{ siteItem.pay_money | moneyFormat }}</td> -->
							</span>
						</tr>
					</table>

					<!--配送方式、留言、优惠券、店铺优惠-->
					<div class="goods-footer">
						<div class="goods-footer-left">
							<div v-if="orderPaymentData.is_virtual == 0">
								<div class="order-cell">
									<div class="tit">
										配送方式
										<span class="ns-text-color">
											<span
												v-if="
													orderPaymentData.delivery[siteItem.site_id].delivery_type == 'store' && orderPaymentData.delivery[siteItem.site_id].store_name
												"
											>
												{{ orderPaymentData.delivery[siteItem.site_id].store_name }}
											</span>
										</span>
									</div>
									<div v-if="siteItem.express_type.length > 0">
										<div
											class="express-item"
											v-for="(deliveryItem, deliveryIndex) in siteItem.express_type"
											:key="deliveryIndex"
											@click="selectDeliveryType(deliveryItem, siteItem.site_id, siteItem.express_type)"
											:class="orderPaymentData.delivery[siteItem.site_id].delivery_type == deliveryItem.name ? 'active' : ''"
											v-if="deliveryItem.name != 'local' "
										>
											{{ deliveryItem.title }}
										</div>
									</div>
									<div v-else-if="memberAddress.length == 0"><div class="box ns-text-color">请先添加收货地址</div></div>
									<div v-else><div class="box ns-text-color">商家未配置配送方式</div></div>
								</div>
							</div>

							<div v-if="siteItem.coupon_list.length > 0">
								<div class="order-cell">
									<div class="tit">优惠券</div>
									<div v-if="siteItem.coupon_id">
										<span class="ns-text-color" @click="openSiteCoupon(siteItem.site_id, siteItem.coupon_list)">已选择优惠券，已优惠</span>
										<span class="ns-text-color" @click="openSiteCoupon(siteItem.site_id, siteItem.coupon_list)">
											<span class="inline">￥</span>
											{{ siteItem.coupon_money }}
										</span>
									</div>

									<div v-else><div class="box ns-text-color" @click="openSiteCoupon(siteItem.site_id, siteItem.coupon_list)">不使用优惠券</div></div>
								</div>
							</div>

							<div>
								<div class="order-cell" v-if="siteItem.promotion_money > 0">
									<span class="tit">店铺优惠</span>
									<div class="box text-overflow">
										<span class="ns-text-color">￥{{siteItem.promotion_money | moneyFormat }}</span>
										
										<!-- <div class="order-cell" v-for="(promotionItem, promotionIndex) in siteItem.promotion" :key="promotionIndex">
											<div class="box text-overflow" v-if="promotionIndex == 'manjian'">
												<span class="ns-text-color">满减：</span>
												<span class="ns-text-color" v-for="(item, index) in promotionItem" :key="index">{{ item.discount_array.desc }}</span>
											</div>

											<div class="box text-overflow" v-if="promotionIndex == 'freeshipping'">
												<span class="ns-text-color">满额包邮：</span>
												<span class="ns-text-color">{{ promotionItem.area_names }} 满{{ promotionItem.price }}包邮</span>
											</div>
										</div> -->
									</div>
								</div>
							</div>
						</div>
						<div class="goods-footer-right">
							<div>
								<div class="order-cell">
									<div class="tit">买家留言</div>
									<div class="box">
										<el-input
											rows="3"
											type="textarea"
											placeholder="留言前建议先与商家协调一致"
											v-model="orderCreateData.buyer_message[siteItem.site_id]"
											class="buyer-message"
											@input="textarea"
											maxlength="140"
											show-word-limit
											resize="none"
										></el-input>
									</div>
								</div>
							</div>
						</div>
						<div class="clear"></div>
					</div>
				</div>
			</div>
		</div>

		<!--平台优惠券-->
		<div class="item-block" v-if="orderPaymentData && orderPaymentData.platform_coupon_list && orderPaymentData.platform_coupon_list.length > 0">
			<div class="block-text">平台优惠券</div>

			<div class="order-cell platform-coupon">
				<div v-if="orderCreateData.platform_coupon_id">
					<span class="ns-text-color" @click="openPlatformCoupon">已选择优惠券，已优惠</span>
					<span class="ns-text-color" @click="openPlatformCoupon">
						<span class="inline">￥</span>
						{{ orderPaymentData.platform_coupon_money }}
					</span>
				</div>

				<div v-else><div class="box ns-text-color" @click="openPlatformCoupon">不使用优惠券</div></div>
			</div>
		</div>

		<!--新人专享优惠券-->
		 <!-- v-if="orderPaymentData && orderPaymentData.new_user_coupon_list && orderPaymentData.new_user_coupon_list.length > 0" -->
		<div class="item-block" v-if="orderPaymentData && orderPaymentData.new_user_coupon_list && orderPaymentData.new_user_coupon_list.length > 0">
			<div class="block-text">新人专享优惠券</div>
			<div class="order-cell platform-coupon">
				<div v-if="orderCreateData.new_user_coupon_id">
					<span class="ns-text-color" @click="openNewCoupon">已选择优惠券，已优惠</span>
					<span class="ns-text-color" @click="openNewCoupon">
						<span class="inline">￥</span>
						{{ orderPaymentData.new_user_coupon_money }}
					</span>
				</div>

				<div v-else><div class="box ns-text-color" @click="openNewCoupon">不使用优惠券</div></div>
			</div>
		</div>

		<!-- 总计 -->
		<div class="item-block">
			<div class="order-statistics">
				<table>
					<tr>
						<td align="right">商品金额：</td>
						<td align="left">￥{{ orderPaymentData.goods_money | moneyFormat }}</td>
					</tr>
					<tr v-if="orderPaymentData.is_virtual == 0">
						<td align="right">运费：</td>
						<td align="left">￥{{ orderPaymentData.delivery_money | moneyFormat }}</td>
					</tr>
					<tr v-if="orderPaymentData.invoice_money > 0">
						<td align="right">税费：</td>
						<td align="left">￥{{ orderPaymentData.invoice_money | moneyFormat }}</td>
					</tr>
					<tr v-if="orderPaymentData.promotion_money > 0">
						<td align="right">优惠：</td>
						<td align="left">￥{{ orderPaymentData.promotion_money | moneyFormat }}</td>
					</tr>
					<tr v-if="orderPaymentData.coupon_money > 0">
						<td align="right">店铺优惠券：</td>
						<td align="left">￥{{ orderPaymentData.coupon_money | moneyFormat }}</td>
					</tr>
					<tr v-if="orderPaymentData.platform_coupon_money > 0">
						<td align="right">平台优惠券：</td>
						<td align="left">￥{{ orderPaymentData.platform_coupon_money | moneyFormat }}</td>
					</tr>
					<tr v-if="orderPaymentData.balance_money > 0">
						<td align="right">使用余额：</td>
						<td align="left">￥{{ orderPaymentData.balance_money | moneyFormat }}</td>
					</tr>
					<tr v-if="orderPaymentData.new_user_coupon_money > 0">
						<td align="right">新人专享优惠金额：</td>
						<td align="left">￥{{ orderPaymentData.new_user_coupon_money | moneyFormat }}</td>
					</tr>
				</table>
			</div>
			<div class="clear"></div>
		</div>

		<!--结算-->
		<div class="item-block">
			<div class="order-submit">
				<div class="order-money">
					共{{ orderPaymentData.goods_num }}件，应付金额：
					<div class="ns-text-color">￥{{ orderPaymentData.pay_money | moneyFormat }}</div>
				</div>
				<el-button type="primary" class="el-button--primary" @click="orderCreate">订单结算</el-button>
			</div>
			<div class="clear"></div>
		</div>

		<!--选择优惠券弹框-->
		<el-dialog title="选择优惠券" :visible.sync="dialogCoupon" width="50%">
			<el-table ref="couponTable" :data="siteCoupon.data" highlight-current-row @row-click="selectCoupon" class="cursor-pointer">
				<el-table-column label="" width="55">
					<template slot-scope="scope">
						<div class="disabled-selected-wrap">
							<el-radio v-model="couponRadio" :label="scope.row"><i></i></el-radio>
						</div>
					</template>
				</el-table-column>
				<el-table-column prop="coupon_name" label="名称" width="200"></el-table-column>
				<el-table-column label="优惠">
					<template slot-scope="scope">
						<span v-if="scope.row.type == 'reward'">{{ scope.row.money }}</span>
						<span v-if="scope.row.type == 'discount'">{{ scope.row.discount }} 折</span>
					</template>
				</el-table-column>
				<el-table-column prop="use" label="使用"></el-table-column>
				<el-table-column prop="time" label="期限"></el-table-column>
			</el-table>
			<br />
			<div class="align-right">
				<el-button @click="dialogCoupon = false">取消</el-button>
				<el-button @click="saveCoupon()" type="primary">确认选择</el-button>
			</div>
		</el-dialog>

		<!--选择平台优惠券弹框-->
		<el-dialog title="选择平台优惠券" :visible.sync="dialogPlatcoupon" width="50%" @close="savePlatformCoupon()">
			<el-table ref="platformCouponTable" :data="orderPaymentData.platform_coupon_list" highlight-current-row @row-click="selectPlatformCoupon" class="cursor-pointer">
				<el-table-column label="" width="55">
					<template slot-scope="scope">
						<div class="disabled-selected-wrap">
							<el-radio v-model="platformCouponRadio" :label="scope.row"><i></i></el-radio>
						</div>
					</template>
				</el-table-column>
				<el-table-column prop="platformcoupon_name" label="名称" width="200"></el-table-column>
				<el-table-column prop="money" label="优惠"></el-table-column>
				<el-table-column label="使用">
					<template slot-scope="scope">
						<span class="ns-text-color-gray ns-font-size-sm" v-if="scope.row.at_least > 0">满{{ scope.row.at_least }}可用</span>
						<span class="ns-text-color-gray ns-font-size-sm" v-else>任意金额可用</span>
					</template>
				</el-table-column>
				<el-table-column label="期限">
					<template slot-scope="scope">
						<span>{{ $util.timeStampTurnTime(scope.row.end_time) }}</span>
					</template>
				</el-table-column>
			</el-table>
			<br />
			<div class="align-right">
				<el-button @click="dialogPlatcoupon = false">取消</el-button>
				<el-button @click="dialogPlatcoupon = false" type="primary">确认选择</el-button>
			</div>
		</el-dialog>

		<!--选择新人专享优惠券弹框-->
		<el-dialog title="选择新人优惠券" :visible.sync="dialogNewcoupon" width="50%" @close="saveNewCoupon()">
			<el-table ref="newCouponTable" :data="orderPaymentData.new_user_coupon_list" highlight-current-row @row-click="selectNewCoupon" class="cursor-pointer">
				<el-table-column label="" width="55">
					<template slot-scope="scope">
						<div class="disabled-selected-wrap">
							<el-radio v-model="newCouponRadio" :label="scope.row"><i></i></el-radio>
						</div>
					</template>
				</el-table-column>
				<el-table-column prop="newcoupon_name" label="名称" width="200">
					<template slot-scope="scope">
						<span class="ns-text-color-gray ns-font-size-sm">{{ scope.row.coupon_name}}</span>
					</template>
					
				</el-table-column>
				<el-table-column prop="money" label="优惠"></el-table-column>
				<el-table-column label="使用">
					<template slot-scope="scope">
						<span class="ns-text-color-gray ns-font-size-sm" v-if="scope.row.money > 0">满{{ scope.row.money }}可用</span>
						<span class="ns-text-color-gray ns-font-size-sm" v-else>任意金额可用</span>
					</template>
				</el-table-column>
				<el-table-column label="期限">
					<template slot-scope="scope">
						<span>{{ $util.timeStampTurnTime(scope.row.expire_time) }}</span>
					</template>
				</el-table-column>
			</el-table>
			<br />
			<div class="align-right">
				<el-button @click="dialogNewcoupon = false">取消</el-button>
				<el-button @click="dialogNewcoupon = false" type="primary">确认选择</el-button>
			</div>
		</el-dialog>


		<!--配送方式  门店 -->
		<el-dialog title="选择门店" :visible.sync="dialogStore" width="50%">
			<el-table ref="singleTable" :data="storeList" highlight-current-row @row-click="selectStore" class="cursor-pointer">
				<el-table-column label="" width="55">
					<template slot-scope="scope">
						<el-radio v-model="storeRadio" :label="scope.row"><i></i></el-radio>
					</template>
				</el-table-column>
				<el-table-column prop="store_name" label="名称" width="160"></el-table-column>
				<el-table-column prop="store_address" label="地址"></el-table-column>
				<el-table-column prop="open_date" label="营业时间"></el-table-column>
			</el-table>
		</el-dialog>

		<!-- 配送方式 外卖 -->
		<el-dialog title="送达时间" :visible.sync="deliveryTime" width="400px">
			<el-form status-icon ref="ruleForm" label-width="100px">
				<el-form-item label="送达时间">
					<el-col :span="11">
						<el-time-picker
							format="HH:mm"
							value-format="HH:mm"
							placeholder="选择时间"
							:value="time"
							v-model="time"
							start="09:01"
							end="21:01"
							@change="bindTimeChange"
						></el-time-picker>
					</el-col>
				</el-form-item>
				<el-form-item label="">
					<el-col :span="20">
						<div class="align-right">
							<el-button size="small" @click="deliveryTime = false">关闭</el-button>
							<el-button type="primary" size="small" @click="setDeliveryTime(siteDelivery.site_id)">确认选择</el-button>
						</div>
					</el-col>
				</el-form-item>
			</el-form>
		</el-dialog>

		<!-- 支付密码 -->
		<el-dialog title="使用余额" :visible.sync="dialogpay" width="350px">
			<template v-if="orderPaymentData.member_account.is_pay_password == 0">
				<p>为了您的账户安全,请您先设置的支付密码</p>
				<p>可到"会员中心","账号安全","支付密码"中设置</p>
				<span slot="footer" class="dialog-footer">
					<el-button size="small" @click="dialogpay = false">暂不设置</el-button>
					<el-button size="small" type="primary" @click="setPayPassword">立即设置</el-button>
				</span>
			</template>
			<el-form v-else status-icon ref="ruleForm" label-width="100px">
				<el-form-item label="支付密码" class="pay-password-item">
					<!--添加一个不可见的input,欺骗浏览器自动填充-->
					<el-input type="password" class="pay-password hide-password" :maxlength="6"></el-input>
					<el-input type="password" class="pay-password" :maxlength="6" v-model="password" @input="input"></el-input>
				</el-form-item>
				<p class="ns-text-color forget-password" @click="setPayPassword">忘记密码</p>
			</el-form>
		</el-dialog>
	</div>
</template>


<script>
import PicZoom from 'vue-piczoom';  
    import {
    checkpaypassword,
    payment,
    calculate,
    orderCreate
} from "@/api/topic"
import {
    addressList,
    saveAddress,
    setDefault,
    deleteAddress,
    addressInfo
} from "@/api/member/member"
import {
    getArea
} from "@/api/address"
import {
    mapGetters
} from "vuex"

export default {
    name: "topic_payment",
    components: {
        PicZoom,
    },
    data: () => {
        var checkMobile = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请输入手机号"))
            } else if (!/^1[3|4|5|6|7|8|9][0-9]{9}$/.test(value)) {
                callback(new Error("手机号格式错误"))
            } else {
                callback()
            }
        }

        return {
            dialogVisible: false,
            memberAddress: [], //收货地址列表
            addressId: 0, //收货地址
            addressForm: {
                id: 0,
                name: "",
                mobile: "",
                telephone: "",
                province_id: "",
                city_id: "",
                district_id: "",
                community_id: "",
                address: "",
                full_address: "",
                is_default: "",
                longitude: "",
                latitude: ""
            },
            pickerValueArray: {},
            cityArr: {},
            districtArr: {},
            addressRules: {
                name: [{
                        required: true,
                        message: "请输入收货人",
                        trigger: "blur"
                    },
                    {
                        min: 1,
                        max: 20,
                        message: "长度在 1 到 20 个字符",
                        trigger: "blur"
                    }
                ],
                mobile: [{
                    required: true,
                    validator: checkMobile,
                    trigger: "change"
                }],
                province: [{
                    required: true,
                    message: "请选择省",
                    trigger: "change"
                }],
                city: [{
                    required: true,
                    message: "请选择市",
                    trigger: "change"
                }],
                district: [{
                    required: true,
                    message: "请选择区/县",
                    trigger: "change"
                }],
                address: [{
                    required: true,
                    message: "请输入详细地址",
                    trigger: "change"
                }]
            },
            isSend: false,
            orderCreateData: {
                is_balance: 0,
                pay_password: "",
                platform_coupon_id: 0,
                new_user_coupon_id: 0,
                buyer_message: {},
            },
            orderPaymentData: {
                goods_money: 0,
                pay_money: 0,
                member_account: {
                    balance: 0,
                    is_pay_password: 0
                },
                platform_coupon_list: [],
                new_user_coupon_list: [],
            },
            dialogCoupon: false,
            siteCoupon: {
                site_id: 0,
                data: []
            },
            siteDelivery: {
                site_id: 0,
                data: []
            },
            dialogStore: false,
            promotionInfo: false,
            storeList: {},
            sitePromotion: [],
            isSub: false,
            dialogpay: false,
            password: "",
            fullscreenLoading: true,
            deliveryTime: false,
            timeTip: "选择配送时间",
            time: null,
            addressShow: false,
            couponRadio: 0,
            storeRadio: 0,
            dialogPlatcoupon: false,
            dialogNewcoupon: false,
            platformCouponRadio: 0,
            newCouponRadio: 0,
        }
    },
    computed: {
        ...mapGetters(["orderCreateGoodsData", "defaultGoodsImage", "city"])
    },
    mounted() {},
    created() {
        this.getMemberAddress()
        this.getOrderPaymentData()
    },

    filters: {
        /**
         * 金额格式化输出
         * @param {Object} money
         */
        moneyFormat(money) {
            return parseFloat(money).toFixed(2)
        },
        /**
         * 店铺优惠摘取
         */
        promotion(data) {
            let promotion = ""
            if (data) {
                Object.keys(data).forEach(key => {
                    promotion += data[key].content + "　"
                })
            }
            return promotion
        }
    },
    methods: {
        //获取收货地址
        getMemberAddress() {
            addressList({
                    page_size: 0
                })
                .then(res => {
                    const {
                        code,
                        message,
                        data
                    } = res
                    if (data && data.list) {
                        let that = this
                        this.memberAddress = data.list
                        data.list.forEach(function(e) {
                            if (e.is_default == 1) {
                                that.addressId = e.id
                            }
                        })
                    }
                })
                .catch(err => {
                    const {
                        code,
                        message,
                        data
                    } = err
                    this.$message.error(message)
                })
        },

        //设置会员收货地址
        setMemberAddress(params) {
            this.addressId = params
            setDefault({
                    id: params
                })
                .then(res => {
                    const {
                        code,
                        message,
                        data
                    } = res
                    this.orderCalculate()
                })
                .catch(err => {
                    const {
                        code,
                        message,
                        data
                    } = err
                    this.$message.error(message)
                })
        },

        //删除会员收货地址
        deleteMemberAddress(params) {
            deleteAddress({
                    id: params
                })
                .then(res => {
                    const {
                        code,
                        message,
                        data
                    } = res
                    if (data) {
                        this.$message({
                            message: message,
                            type: "success"
                        })
                        this.getMemberAddress()
                    } else {
                        this.$message({
                            message: message,
                            type: "warning"
                        })
                    }
                })
                .catch(err => {
                    this.$message.error(err.message)
                })
        },

        //打开添加收货地址弹出层
        addAddressShow() {
            this.dialogVisible = true
            this.addressForm.id = 0
            this.addressForm.name = ""
            this.addressForm.mobile = ""
            this.addressForm.telephone = ""
            this.addressForm.province_id = ""
            this.addressForm.city_id = ""
            this.addressForm.district_id = ""
            this.addressForm.community_id = ""
            this.addressForm.address = ""
            this.addressForm.full_address = ""
            this.addressForm.is_default = ""
            this.addressForm.longitude = ""
            this.addressForm.latitude = ""
                // this.$nextTick(() => {
                // 	this.$refs.form.resetFields();
                // });
            this.cityArr = {}
            this.districtArr = {}
            this.getAddress(0)
        },

        //获取地址
        getAddress(type) {
            let pid = 0
            let that = this
            switch (type) {
                case 0:
                    //加载省
                    pid = 0
                    break
                case 1:
                    //加载市
                    pid = this.addressForm.province_id
                    that.cityArr = {}
                    that.districtArr = {}
                    this.addressForm.city_id = ""
                    this.addressForm.district_id = ""
                    break
                case 2:
                    //加载区县
                    pid = this.addressForm.city_id
                    that.districtArr = {}
                    this.addressForm.district_id = ""
                    break
            }

            getArea({
                    pid: pid
                })
                .then(res => {
                    const {
                        code,
                        message,
                        data
                    } = res
                    if (data) {
                        switch (type) {
                            case 0:
                                that.pickerValueArray = data
                                break
                            case 1:
                                //加载市
                                that.cityArr = data
                                break
                            case 2:
                                //加载区县
                                that.districtArr = data
                                break
                        }
                    }
                })
                .catch(err => {
                    const {
                        code,
                        message,
                        data
                    } = err
                    this.$message.error(message)
                })
        },

        //编辑地址 初始化
        initAddress(type) {
            let pid = 0
            let that = this
            switch (type) {
                case 0:
                    //加载省
                    pid = 0
                    break
                case 1:
                    //加载市
                    pid = this.addressForm.province_id
                    that.cityArr = {}
                    that.districtArr = {}
                    break
                case 2:
                    //加载区县
                    pid = this.addressForm.city_id
                    that.districtArr = {}
                    break
            }

            getArea({
                    pid: pid
                })
                .then(res => {
                    const {
                        code,
                        message,
                        data
                    } = res
                    if (data) {
                        switch (type) {
                            case 0:
                                that.pickerValueArray = data
                                break
                            case 1:
                                //加载市
                                that.cityArr = data
                                break
                            case 2:
                                //加载区县
                                that.districtArr = data
                                break
                        }
                    }
                })
                .catch(err => {
                    const {
                        code,
                        message,
                        data
                    } = err
                    this.$message.error(message)
                })
        },

        //新增/编辑收货地址
        addmemberAddress(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    if (this.isSend) {
                        return false
                    }

                    if (!this.addressForm.id) {
                        this.addressForm.full_address = this.$refs.province.selectedLabel + "-" + this.$refs.city.selectedLabel + "-" +
                            this.$refs.district.selectedLabel
                        let data = {
                            name: this.addressForm.name,
                            mobile: this.addressForm.mobile,
                            telephone: this.addressForm.telephone,
                            province_id: this.addressForm.province_id,
                            city_id: this.addressForm.city_id,
                            district_id: this.addressForm.district_id,
                            community_id: "",
                            address: this.addressForm.address,
                            full_address: this.addressForm.full_address,
                            longitude: this.addressForm.longitude,
                            latitude: this.addressForm.latitude,
                            is_default: this.addressForm.is_default,
                            url: 'add'
                        }

                        if (!data.province_id) {
                            this.$message({
                                message: "请选择省",
                                type: "warning"
                            })
                            return false
                        }
                        if (!data.city_id) {
                            this.$message({
                                message: "请选择市",
                                type: "warning"
                            })
                            return false
                        }
                        if (!data.district_id) {
                            this.$message({
                                message: "请选择区/县",
                                type: "warning"
                            })
                            return false
                        }
                        this.isSend = true

                        saveAddress(data)
                            .then(res => {
                                const {
                                    code,
                                    message,
                                    data
                                } = res
                                if (data) {
                                    this.setMemberAddress(data)

                                    this.$message({
                                        message: message,
                                        type: "success"
                                    })
                                    this.dialogVisible = false
                                    this.getMemberAddress()
                                    this.getOrderPaymentData()
                                } else {
                                    this.$message({
                                        message: message,
                                        type: "warning"
                                    })
                                }
                                this.isSend = false
                            })
                            .catch(err => {
                                const {
                                    code,
                                    message,
                                    data
                                } = err
                                this.$message.error(message)
                            })
                    } else {
                        this.addressForm.full_address = this.$refs.province.selectedLabel + "-" + this.$refs.city.selectedLabel + "-" +
                            this.$refs.district.selectedLabel
                        let data = this.addressForm
                        if (!data.province_id) {
                            this.$message({
                                message: "请选择省",
                                type: "warning"
                            })
                            return false
                        }
                        if (!data.city_id) {
                            this.$message({
                                message: "请选择市",
                                type: "warning"
                            })
                            return false
                        }
                        if (!data.district_id) {
                            this.$message({
                                message: "请选择区/县",
                                type: "warning"
                            })
                            return false
                        }
                        this.isSend = true
                        this.setMemberAddress(data.id);
                        data.url = "edit";
                        saveAddress(data)
                            .then(res => {
                                const {
                                    code,
                                    message,
                                    data
                                } = res
                                if (data) {
                                    this.$message({
                                        message: message,
                                        type: "success"
                                    })
                                    this.dialogVisible = false
                                    this.getMemberAddress()
                                    this.getOrderPaymentData()
                                } else {
                                    this.$message({
                                        message: message,
                                        type: "warning"
                                    })
                                }
                                this.isSend = false
                            })
                            .catch(err => {
                                const {
                                    code,
                                    message,
                                    data
                                } = err
                                this.$message.error(message)
                            })
                    }
                } else {
                    return false
                }
            })
        },

        //编辑收货地址
        editAddress(id) {
            addressInfo({
                    id: id
                })
                .then(res => {
                    const {
                        code,
                        message,
                        data
                    } = res
                    this.addressForm = {
                        id: data.id,
                        name: data.name,
                        mobile: data.mobile,
                        telephone: data.telephone,
                        province_id: data.province_id,
                        city_id: "",
                        district_id: "",
                        community_id: "",
                        address: data.address,
                        full_address: data.full_address,
                        is_default: data.is_default,
                        longitude: data.longitude,
                        latitude: data.latitude
                    }
                    this.initAddress(0)
                    this.initAddress(1)
                    this.addressForm.city_id = data.city_id
                    this.initAddress(2)
                    this.addressForm.district_id = data.district_id

                    this.dialogVisible = true
                })
                .catch(err => {
                    const {
                        code,
                        message,
                        data
                    } = err
                    this.$message.error(message)
                })
        },

        /**
         * 获取订单初始化数据
         */
        getOrderPaymentData() {
            this.orderCreateData = this.orderCreateGoodsData

            if (!this.orderCreateData) {
                this.$message({
                    message: "未获取到创建订单所需数据！", //提示的信息
                    type: "warning",
                    offset: 225,
                    duration: 3000,
                    onClose: () => {
                        this.$router.go(-1)
                        return false
                    }
                })
                return
            }
            this.orderCreateData.web_city = this.city ? this.city.id : 0
            payment(this.orderCreateData)
                .then(res => {
                    const {
                        code,
                        message,
                        data
                    } = res

                    if (code >= 0) {
                        this.orderPaymentData = data
                        let siteItem = this.orderPaymentData.shop_goods_list;
                        let shop_goods_list = {};
                        shop_goods_list[siteItem.site_id] = siteItem;
                        this.orderPaymentData.shop_goods_list = shop_goods_list;
                        this.handlePaymentData()
                    } else {
                        this.$message({
                            message: "未获取到创建订单所需数据！", //提示的信息
                            type: "warning",
                            offset: 225,
                            duration: 3000,
                            onClose: () => {
                                this.$router.go(-1)
                                return false
                            }
                        })
                        return
                    }
                })
                .catch(err => {
                    const {
                        code,
                        message,
                        data
                    } = err
                    this.$message.error(message)
                })
        },
        /**
         * 处理结算订单数据
         */
        handlePaymentData() {
           
            this.orderCreateData.delivery = {}
            this.orderCreateData.coupon = {}
            this.orderCreateData.buyer_message = {}

            this.orderCreateData.is_balance = 0
            this.orderCreateData.pay_password = ""

            var data = this.orderPaymentData

            Object.keys(data.shop_goods_list).forEach((key, index) => {
                let  siteItem  = data.shop_goods_list[key];

                // 店铺配送方式
                this.orderCreateData.delivery[key] = {}
                if (siteItem.express_type[0] != undefined) {
                    this.orderCreateData.delivery[key].delivery_type = siteItem.express_type[0].name
                    this.orderCreateData.delivery[key].delivery_type_name = siteItem.express_type[0].title
                    this.orderCreateData.delivery[key].store_id = 0
                        // 如果是门店配送
                    if (siteItem.express_type[0].name == "store") {
                        if (siteItem.express_type[0].store_list[0] != undefined) {
                            this.orderCreateData.delivery[key].store_id = siteItem.express_type[0].store_list[0].store_id
                        }
                    }
                }
                
                // 店铺优惠券
                this.orderCreateData.coupon[key] = {}
                if (siteItem.coupon_list[0] != undefined) {
                    this.orderCreateData.coupon[key].coupon_id = siteItem.coupon_list[0].coupon_id
                    this.orderCreateData.coupon[key].coupon_money = siteItem.coupon_list[0].money

                }

                this.orderCreateData.buyer_message[key] = ""
            })
            
            if (this.orderPaymentData.is_virtual) {
                this.orderCreateData.member_address = {
                    mobile: ""
                }
            }

            Object.assign(this.orderPaymentData, this.orderCreateData)

            this.orderPaymentData.new_user_coupon_id = 0;
            this.orderCreateData.new_user_coupon_id = 0;
            this.orderPaymentData.new_user_coupon_money = '0.00';
            this.orderCreateData.new_user_coupon_money = '0.00';

            this.orderCalculate()
        },

        /**
         * 订单计算
         */
        orderCalculate() {
            this.fullscreenLoading = true
            var data = this.$util.deepClone(this.orderCreateData)
            data.delivery = JSON.stringify(data.delivery)
            data.coupon = JSON.stringify(data.coupon)
            data.member_address = JSON.stringify(data.member_address)

            calculate(data)
                .then(res => {
                    const {
                        code,
                        message,
                        data
                    } = res
                    if (code >= 0) {
                        this.orderPaymentData.delivery_money = res.data.delivery_money
                        this.orderPaymentData.coupon_money = res.data.coupon_money
                        this.orderPaymentData.invoice_money = res.data.invoice_money
                        this.orderPaymentData.promotion_money = res.data.promotion_money
                        this.orderPaymentData.order_money = res.data.order_money
                        this.orderPaymentData.balance_money = res.data.balance_money
                        this.orderPaymentData.pay_money = res.data.pay_money
                        this.orderPaymentData.goods_money = res.data.goods_money

                        let siteItem = res.data.shop_goods_list;
                        let shop_goods_list = {};
                        shop_goods_list[siteItem.site_id] = siteItem;
                        res.data.shop_goods_list = shop_goods_list;
                        
                        Object.keys(res.data.shop_goods_list).forEach((key, index) => {
                            let siteItem = res.data.shop_goods_list[key]

                            this.orderPaymentData.shop_goods_list[key].pay_money = siteItem.pay_money
                            this.orderPaymentData.shop_goods_list[key].coupon_money = siteItem.coupon_money;
                            this.orderPaymentData.shop_goods_list[key].coupon_id = siteItem.coupon_id;
                        })

                        console.log(7777);
                    } else {
                        this.$message({
                            message: message, //提示的信息
                            type: "warning",
                            offset: 225,
                            duration: 3000,
                            onClose: () => {
                                this.$router.go(-1)
                                return false
                            }
                        })
                        return
                    }
                    this.fullscreenLoading = false
                })
                .catch(err => {
                    const {
                        code,
                        message,
                        data
                    } = err
                    this.$message.error(message)
                    this.fullscreenLoading = false
                })
        },

        /**
         * 选择配送方式
         */
        selectDeliveryType(data, siteId, deliveryData) {
            this.tempData = {
                delivery: this.$util.deepClone(this.orderPaymentData.delivery)
            }

            this.siteDelivery.site_id = siteId
            this.siteDelivery.data = deliveryData

            this.orderCreateData.delivery[siteId].delivery_type = data.name
            this.orderCreateData.delivery[siteId].delivery_type_name = data.title
            if (data.name == "store") {
                // 如果是门店配送
                let row = '';
                let that = this;
                data.store_list.forEach(function(e, i) {
                    data.store_list[i]["store_address"] = e.full_address + e.address
                })

                if (!this.orderCreateData.delivery[siteId].store_id && data.store_list[0] != undefined) {
                    this.orderCreateData.delivery[siteId].store_id = data.store_list[0].store_id
                    this.orderCreateData.delivery[siteId].store_name = data.store_list[0].store_name
                }

                data.store_list.forEach(function(e, i) {
                    if (that.orderCreateData.delivery[siteId].store_id == e.store_id) {
                        row = e
                    }
                })

                this.dialogStore = true
                this.storeList = data.store_list

                setTimeout(function() {
                    that.setStore(row)
                    that.storeRadio = row;
                }, 50)

            } else if (data.name == "local") {
                this.deliveryTime = true
                this.time = this.orderCreateData.delivery[siteId].buyer_ask_delivery_time;
            }
            Object.assign(this.orderPaymentData, this.orderCreateData)
            this.orderCalculate()
            this.$forceUpdate()
        },

        /**
         * 设置选择自提点
         * @param {Object} item
         */
        setStore(row) {
            this.$refs.singleTable.setCurrentRow(row)
        },

        /**
         * 选择自提点
         * @param {Object} item
         */
        selectStore(item) {
            if (!item) return;
            let store_id = item.store_id
            this.dialogStore = false
            this.orderCreateData.delivery[this.siteDelivery.site_id].store_id = store_id
            this.orderCreateData.delivery[this.siteDelivery.site_id].store_name = item.store_name
            Object.assign(this.orderPaymentData, this.orderCreateData)
            this.storeRadio = item
            this.orderCalculate()
            this.$forceUpdate()
        },

        /**
         * 显示平台优惠券信息
         * @param {Object} siteId
         * @param {Object} couponData
         */
        openPlatformCoupon() {
            let row = 0
            let that = this;
            this.dialogPlatcoupon = true

            this.orderPaymentData.platform_coupon_list.forEach(function(e, i) {
                if (e.platformcoupon_id == that.orderCreateData.platform_coupon_id) {
                    row = e
                }
            })

            setTimeout(function() {
                that.setPlatformCurrent(row)
                that.platformCouponRadio = row;
            }, 50)
        },

        /**
         * 显示新人专享优惠券信息
         * @param {Object} siteId
         * @param {Object} couponData
         */
        openNewCoupon() {
            let row = 0
            let that = this;
            this.dialogNewcoupon = true

            this.orderPaymentData.new_user_coupon_list.forEach(function(e, i) {
                if (e.coupon_id == that.orderCreateData.new_user_coupon_id) {
                    row = e
                }
            })


            setTimeout(function() {
                that.setNewCurrent(row)
                that.newCouponRadio = row;
            }, 50)
        },


        /**
         * 取消选择优惠券
         * @param {Object} item
         */
        setPlatformCurrent(row) {
            this.$refs.platformCouponTable.setCurrentRow(row)
            if (row == undefined) {
                this.orderCalculate()
            }
        },
        /**
         * 确认选择优惠券
         * @param {Object} item
         */
        savePlatformCoupon() {
            this.dialogPlatcoupon = false
            this.orderCalculate()
        },


        /**
         * 取消选择新人专享优惠券
         * @param {Object} item
         */
        setNewCurrent(row) {
            this.$refs.newCouponTable.setCurrentRow(row)
            if (row == undefined) {
                this.orderCalculate()
            }
        },
        /**
         * 确认选择新人专享优惠券
         * @param {Object} item
         */
        saveNewCoupon() {
            this.dialogNewcoupon = false
            this.orderCalculate()
        },

        /**
         * 选择优惠券
         * @param {Object} item
         */
        selectPlatformCoupon(item) {
            this.orderPaymentData.new_user_coupon_id = 0;
            this.orderCreateData.new_user_coupon_id = 0;
            this.orderPaymentData.new_user_coupon_money = "0.00";
            this.orderCreateData.new_user_coupon_money = "0.00";
            this.newCouponRadio = '';
            if (!item) {
                this.orderPaymentData.platform_coupon_id = 0;
                this.orderCreateData.platform_coupon_id = 0;
                this.orderPaymentData.platform_coupon_money = "0.00";
                this.orderCreateData.platform_coupon_money = "0.00";
                this.platformCouponRadio = '';
            } else {
                if (this.orderCreateData.platform_coupon_id != item.platformcoupon_id) {
                    this.orderPaymentData.platform_coupon_id = item.platformcoupon_id;
                    this.orderCreateData.platform_coupon_id = item.platformcoupon_id;
                    this.orderPaymentData.platform_coupon_money = item.money;
                    this.orderCreateData.platform_coupon_money = item.money;

                    this.platformCouponRadio = item;
                } else {
                    this.platformCouponRadio = '';
                    this.orderPaymentData.platform_coupon_id = 0;
                    this.orderCreateData.platform_coupon_id = 0;
                    this.orderPaymentData.platform_coupon_money = "0.00";
                    this.orderCreateData.platform_coupon_money = "0.00";
                }
            }
            Object.assign(this.orderPaymentData, this.orderCreateData);
            this.$forceUpdate()
        },

        /**
         * 选择新人专享优惠券
         * @param {Object} item
         */
        selectNewCoupon(item) {
            this.orderPaymentData.platform_coupon_id = 0;
            this.orderCreateData.platform_coupon_id = 0;
            this.orderPaymentData.platform_coupon_money = "0.00";
            this.orderCreateData.platform_coupon_money = "0.00";
            this.platformCouponRadio = '';
            if (!item) {
                this.orderPaymentData.new_user_coupon_id = 0;
                this.orderCreateData.new_user_coupon_id = 0;
                this.orderPaymentData.new_user_coupon_money = "0.00";
                this.orderCreateData.new_user_coupon_money = "0.00";
                this.newCouponRadio = '';
            } else {
                if (this.orderCreateData.new_user_coupon_id != item.coupon_id) {
                    this.orderPaymentData.new_user_coupon_id = item.coupon_id;
                    this.orderCreateData.new_user_coupon_id = item.coupon_id;
                    this.orderPaymentData.new_user_coupon_money = item.money;
                    this.orderCreateData.new_user_coupon_money = item.money;

                    this.newCouponRadio = item;
                } else {
                    this.newCouponRadio = '';
                    this.orderPaymentData.new_user_coupon_id = 0;
                    this.orderCreateData.new_user_coupon_id = 0;
                    this.orderPaymentData.new_user_coupon_money = "0.00";
                    this.orderCreateData.new_user_coupon_money = "0.00";
                }
            }
            Object.assign(this.orderPaymentData, this.orderCreateData);
            this.$forceUpdate()
        },


        /**
         * 显示店铺优惠券信息
         * @param {Object} siteId
         * @param {Object} couponData
         */
        openSiteCoupon(siteId, couponData) {
            this.tempData = {
                coupon: this.$util.deepClone(this.orderPaymentData.coupon)
            }
            let row = 0
            let that = this
            this.siteCoupon.site_id = siteId
            couponData.forEach(function(e, i) {
                if (e.at_least > 0) {
                    couponData[i]["use"] = "满" + e.at_least + "可用"
                } else {
                    couponData[i]["use"] = "任意金额可用"
                }
                couponData[i]["time"] = timeStampTurnTime(e.end_time)

                if (e.coupon_id == that.orderCreateData.coupon[that.siteCoupon.site_id].coupon_id) {
                    row = e
                }
            })
            this.siteCoupon.data = couponData
            this.dialogCoupon = true

            setTimeout(function() {
                that.setCurrent(row)
                that.couponRadio = row;
            }, 50)
        },
        /**
         * 选择优惠券
         * @param {Object} item
         */
        selectCoupon(item) {
            if (!item) {
                this.orderCreateData.coupon[this.siteCoupon.site_id].coupon_id = 0
                this.orderCreateData.coupon[this.siteCoupon.site_id].coupon_money = "0.00"
                this.couponRadio = '';
            } else {
                if (this.orderCreateData.coupon[this.siteCoupon.site_id].coupon_id != item.coupon_id) {
                    this.orderCreateData.coupon[this.siteCoupon.site_id].coupon_id = item.coupon_id
                    this.orderCreateData.coupon[this.siteCoupon.site_id].coupon_money = item.money
                    this.couponRadio = item;
                } else {
                    this.couponRadio = '';
                    this.orderCreateData.coupon[this.siteCoupon.site_id].coupon_id = 0
                    this.orderCreateData.coupon[this.siteCoupon.site_id].coupon_money = "0.00"
                }
            }
            Object.assign(this.orderPaymentData, this.orderCreateData)

            this.$forceUpdate()
        },

        /**
         * 取消选择优惠券
         * @param {Object} item
         */
        setCurrent(row) {
            this.$refs.couponTable.setCurrentRow(row)
            if (row == undefined) {
                this.orderCalculate()
            }
        },
        /**
         * 确认选择优惠券
         * @param {Object} item
         */
        saveCoupon() {
            this.dialogCoupon = false
            this.orderCalculate()
        },

        /**
         * 显示店铺优惠信息
         * @param {Object} data
         */
        openSitePromotion(data) {
            this.sitePromotion = data
            if (this.promotionInfo) {
                this.promotionInfo = false
            } else {
                this.promotionInfo = true
            }
        },

        /**
         * 是否使用余额
         */
        useBalance(type) {
            if (this.orderCreateData.is_balance) this.orderCreateData.is_balance = 0
            else this.orderCreateData.is_balance = 1
            this.orderCalculate()
            this.$forceUpdate()
        },

        orderCreate() {
            if (this.verify()) {
                if (this.isSub) return
                this.isSub = true

                var loading = this.$loading({
                    lock: true,
                    text: "订单提交中...",
                    spinner: "el-icon-loading",
                    background: "rgba(0, 0, 0, 0.7)"
                })
                var data = this.$util.deepClone(this.orderCreateData)
                data.delivery = JSON.stringify(data.delivery)
                data.coupon = JSON.stringify(data.coupon)
                data.member_address = JSON.stringify(data.member_address)
                data.buyer_message = JSON.stringify(data.buyer_message)

                orderCreate(data)
                    .then(res => {
                        const {
                            code,
                            message,
                            data
                        } = res
                        loading.close()
                        if (code >= 0) {
                            this.$store.dispatch("order/removeOrderCreateData", "")
                            if (this.orderPaymentData.pay_money == 0) {
                                this.$router.push({
                                    path: "/result",
                                    query: {
                                        code: data
                                    }
                                })
                            } else {
                                this.$router.push({
                                    path: "/pay",
                                    query: {
                                        code: data
                                    }
                                })
                            }
                        } else {
                            this.$message({
                                message: message,
                                type: "warning"
                            })
                        }
                    })
                    .catch(err => {
                        loading.close()
                        this.isSub = false
                        const {
                            code,
                            message,
                            data
                        } = err
                        this.$message.error(message)
                    })
            }
        },
        /**
         * 订单验证
         */
        verify() {
            if (this.orderPaymentData.is_virtual == 1) {
                if (!this.orderCreateData.member_address.mobile.length) {
                    this.$message({
                        message: "请输入您的手机号码",
                        type: "warning"
                    })
                    return false
                }
                var reg = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/
                if (!reg.test(this.orderCreateData.member_address.mobile)) {
                    this.$message({
                        message: "请输入正确的手机号码",
                        type: "warning"
                    })
                    return false
                }
            }

            if (this.orderPaymentData.is_virtual == 0) {
                if (!this.orderPaymentData.member_address) {
                    this.$message({
                        message: "请先选择您的收货地址",
                        type: "warning"
                    })

                    return false
                }

                let deliveryVerify = true

                for (let key in this.orderCreateData.delivery) {
                    if (JSON.stringify(this.orderCreateData.delivery[key]) == "{}") {
                        deliveryVerify = false
                        this.$message({
                            message: '店铺"' + this.orderPaymentData.shop_goods_list[key].site_name + '"未设置配送方式',
                            type: "warning"
                        })

                        break
                    }
                    if (this.orderCreateData.delivery[key].delivery_type == "store" && this.orderCreateData.delivery[key].store_id ==
                        0) {
                        deliveryVerify = false
                        this.$message({
                            message: '店铺"' + this.orderPaymentData.shop_goods_list[key].site_name + '"没有可提货的门店,请选择其他配送方式',
                            type: "warning"
                        })

                        break
                    }
                }
                if (!deliveryVerify) return false
            }

            if (this.orderCreateData.is_balance == 1 && this.orderCreateData.pay_password == "") {
                this.dialogpay = true
                return false
            }
            return true
        },
        /**
         * 支付密码输入
         */
        input() {
            if (this.password.length == 6) {
                var loading = this.$loading({
                    lock: true,
                    text: "支付中",
                    spinner: "el-icon-loading",
                    background: "rgba(0, 0, 0, 0.7)"
                })

                checkpaypassword({
                        pay_password: this.password
                    })
                    .then(res => {
                        const {
                            code,
                            message,
                            data
                        } = res
                        loading.close()
                        if (code >= 0) {
                            this.orderCreateData.pay_password = this.password
                            this.orderCreate()
                            this.dialogpay = false
                        } else {
                            this.$message({
                                message: message,
                                type: "warning"
                            })
                        }
                    })
                    .catch(err => {
                        loading.close()
                        const {
                            code,
                            message,
                            data
                        } = err
                        this.$message.error(message)
                    })
            }
        },

        textarea() {
            this.$forceUpdate()
        },
        bindTimeChange(time) {
            this.time = time
        },
        setDeliveryTime(site_id) {
            this.deliveryTime = false
            this.orderCreateData.delivery[site_id].buyer_ask_delivery_time = this.time;
        },
        imageError(index) {
            this.orderPaymentData.shop_goods_list.goods_list[index].sku_image = this.defaultGoodsImage
        },
        setPayPassword() {
            this.$router.pushToTab("/member/security");
        }
    }
}
</script>


<style lang="scss" scoped>
.clear {
    clear: both;
}

.item-block {
    padding: 0 15px 1px;
    margin: 10px 0;
    border-radius: 0;
    border: none;
    background: #ffffff;
    .block-text {
        border-color: #eeeeee;
        color: $ns-text-color-black;
        padding: 7px 0;
        border-bottom: 1px;
    }
}

//收货地址
.address-desc {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}

.address-item {
    width: 23%;
    border: 3px solid #eee;
    box-sizing: border-box;
    margin: 1%;
    height: 150px;
    cursor: pointer;
    float: left;
    .add-address {
        padding: 10px;
        text-align: center;
        line-height: 120px;
    }
    .address-info {
        padding: 10px;
        height: 100%;
        box-sizing: border-box;
        position: relative;
        .options {
            position: absolute;
            right: 0;
            top: 10px;
            width: 50%;
            text-align: right;
            div {
                display: inline-block;
                margin-right: 10px;
                color: #999999;
            }
        }
        .address-name {
            color: $base-color;
            padding: 0px 0 6px;
            border-bottom: 1px solid #eee;
            width: 100%;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            padding-right: 71px;
            box-sizing: border-box;
            line-height: 25px;
        }
        .address-mobile {
            padding: 10px 0 4px;
        }
    }
}

.address-item.active {
    background-color: #fffbf9;
    border-color: $base-color;
}

//支付方式
.pay-type-list {
    padding: 20px 0;
}

.pay-type-item {
    display: inline-block;
    border: 2px solid #eeeeee;
    padding: 5px 20px;
    margin-right: 20px;
    cursor: pointer;
}

.pay-type-item.active {
    border-color: $base-color;
}

.mobile-wrap {
    width: 300px;
}

//商品列表
.goods-list {
    padding: 15px 0;
    .store-icon {
        margin-right: 5px;
        font-size: 16px;
    }
    .shop-icon {
        font-size: 18px;
    }
    table {
        width: 100%;
    }
    .goods-heads {
        .goods-info {
            display: flex;
            width: 500px;
            .goods-info-left {
                width: 60px;
                height: 60px;
                float: left;
                .goods-img {
                    width: 60px;
                    height: 60px;
                }
            }
            .goods-info-right {
                float: left;
                height: 60px;
                margin-left: 10px;
                color: $base-color;
                width: 80%;
                .goods-name {
                    line-height: 20px;
                    padding-top: 10px;
                    display: -webkit-box;
                    -webkit-box-orient: vertical;
                    -webkit-line-clamp: 2;
                    overflow: hidden;
                }
                .goods-spec {
                    color: #999;
                }
            }
        }
    }
    .goods-info-left {
        width: 60px;
        height: 60px;
        float: left;
        .goods-img {
            width: 60px;
            height: 60px;
        }
    }
    .goods-info-right {
        float: left;
        height: 60px;
        margin-left: 10px;
        color: $base-color;
        width: 80%;
        .goods-name {
            line-height: 20px;
            padding-top: 10px;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            overflow: hidden;
        }
        .goods-spec {
            color: #999;
        }
    }
    .goods-footer {
        background-color: #f3fbfe;
        padding: 10px;
        margin-top: 10px;
        .goods-footer-right {
            width: 50%;
            float: left;
        }
        .goods-footer-left {
            width: 50%;
            float: left;
        }
        .order-cell {
            cursor: pointer;
            .tit {
                padding: 5px 0 0 0;
            }
        }
        .express-item {
            margin-top: 5px;
            display: inline-block;
            border: 2px solid #eeeeee;
            padding: 3px 10px;
            margin-right: 20px;
            cursor: pointer;
        }
        .express-item.active {
            border-color: $base-color;
        }
    }
}

.promotion-show {
    color: #999;
}

.align-right {
    text-align: right;
}

//统计
.order-statistics {
    float: right;
    padding: 10px;
    // color: #999;
}

.order-submit {
    float: right;
    padding: 10px;
}

.order-money {
    display: inline-block;
    margin-right: 20px;
    div {
        display: inline-block;
        font-size: $ns-font-size-lg;
    }
}

.buyer-message.el-textarea {
    width: 400px;
    margin-top: 10px;
}

.address-open {
    cursor: pointer;
    font-size: 13px;
    padding: 0 0 10px 10px;
}

.pay-password-item {
    margin-bottom: 10px;
}

.pay-password {
    width: 80%;
}

.pay-password.hide-password {
    position: fixed;
    top: -9999px;
}

.platform-coupon {
    cursor: pointer;
}

.forget-password {
    text-align: right;
    margin-right: 45px;
    cursor: pointer;
}

.disabled-selected-wrap {
    pointer-events: none;
    cursor: pointer;
}

.cursor-pointer {
    cursor: pointer;
}
</style>

